package edu.hawaii.wattwaiter.wattdepotcommands;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

/**
 * Tests the GoogleChartMaker to ensure that it is working as intended.
 * @author David Mau
 */
public class TestGoogleChartMaker {
  // Variables for repeated use to silence PMD.
  private String oahuGrid = "SIM_OAHU_GRID";
  private String carbon = "carbon";
  private String energy = "energy";
  private String hour = "hour";
  private String chartStart = "http://chart.apis.google.com/chart?" +
      "cht=lc" +
      "&amp;chs=600x200" +
      "&amp;chxt=x,y";
  private String oahuGridLabel = "&amp;chdl=SIM_OAHU_GRID";
  private String blackGraphLine = "&amp;chco=000000";
  private String startLabels = "&amp;chxl=";
  private String hoursOfTheDay = "0:|Mid.|1|2|3|4|5|6|7|8|9|10|11|Noon|1|2|3|4|5|6|7|8|9|10|11|";

  /**
   * Tests the basic operation of the GoogleChartMaker.
   */
  @Test
  public void testGoogleChartMaker() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-10-25", "2009-10-26",
        oahuGrid, hour, false, false );
    
    // Ready the expected results.
    String expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( oahuGridLabel );
    expected = expected.concat( blackGraphLine );
    expected = expected.concat( startLabels );
    expected = expected.concat( hoursOfTheDay );
    expected = expected.concat( "1:|0|273413|546825|820238|1093650" );
    expected = expected.concat( "&amp;chds=0,1093650" );
    expected = expected.concat( "&amp;chd=t:802164,798676,796932,798676,801292,823092,860588," );
    expected = expected.concat( "894596,928604,967844,1000567,1039425,1077875,1093650,1090950," );
    expected = expected.concat( "1086450,1087350,1087350,1084525,1076325,1030705,959996," );
    expected = expected.concat( "892852,828324" );
    
    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking normal hour-based chart creation.", 
        results.compareTo(expected), 0 );
  }
  
  /**
   * Check for correct response to being given a null source.
   */
  @Test
  public void testNullSourceHandling() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-10-27", "2009-10-28",
        null, hour, false, false );
    
    assertEquals( "Checking for correct response to a null source.",
        results.compareTo( GoogleChartMaker.errorImage ), 0 );
  }
  
  /**
   * Check for correct response to being given a bad date.
   */
  @Test
  public void testBadSourceHandling() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-10-30", "2009-10-30",
        "SIM_IPP", hour, false, false );
    
    assertEquals( "Checking for correct response to a null source.",
        results.compareTo( GoogleChartMaker.timeImage ), 0 );
  }
  
  /**
   * Test the normal operation of GoogleChartMaker for charting energy.
   */
  @Test
  public void testEnergyCharting() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( energy, "2009-11-15", "2009-11-16",
        oahuGrid, hour, false, false );
    
    // Ready the expected results.
    String expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( oahuGridLabel );
    expected = expected.concat( blackGraphLine );
    expected = expected.concat( startLabels );
    expected = expected.concat( hoursOfTheDay );
    expected = expected.concat( "1:|0|166|331|497|663" );
    expected = expected.concat( "&amp;chds=0,663" );
    expected = expected.concat( "&amp;chd=t:502,500,499,500,502,514,536,555,575,597,616,636," );
    expected = expected.concat( "655,663,661,659,659,659,658,654,631,593,554,517" );

    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking normal hour-based chart creation.", 
        results.compareTo(expected), 0 );
  }
  
  /**
   * Test normal operation of the day option for creating charts.
   */
  @Test
  public void testDayOption() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-11-02", 
        "2009-11-12", oahuGrid, "day", false, false );
    
    // Ready the expected results.
    String expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( oahuGridLabel );
    expected = expected.concat( blackGraphLine );
    expected = expected.concat( startLabels );
    expected = expected.concat( "0:|11-2|11-3|11-4|11-5|11-6|11-7|11-8|11-9|11-10|11-11|" );
    expected = expected.concat( "1:|0|7489868|14979736|22469604|29959472" );
    expected = expected.concat( "&amp;chds=0,29959472" );
    expected = expected.concat( "&amp;chd=t:24565908,24670370,29959472,29959472," );
    expected = expected.concat( "29855010,22908808,22908808,24565908,24670370,29855010" );
    
    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking carbon day-based chart creation.", 
        results.compareTo(expected), 0 );
    
    // Get the results from the class.
    results = test.getChart( energy, "2009-11-02", 
        "2009-11-12", oahuGrid, "day", false, false );
    
    // Ready the expected results.
    expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( oahuGridLabel );
    expected = expected.concat( blackGraphLine );
    expected = expected.concat( startLabels );
    expected = expected.concat( "0:|11-2|11-3|11-4|11-5|11-6|11-7|11-8|11-9|11-10|11-11|" );
    expected = expected.concat( "1:|0|3691|7382|11073|14764" );
    expected = expected.concat( "&amp;chds=0,14764" );
    expected = expected.concat( "&amp;chd=t:14764,14764,14764,14764,14764,14089,14089," );
    expected = expected.concat( "14764,14764,14764" );

    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking energy day-based chart creation.", 
        results.compareTo(expected), 0 );
  }
  
  /**
   * Test normal operation of the week option for creating charts.
   */
  @Test
  public void testWeekOption() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-11-01", 
        "2009-12-01", oahuGrid, "week", false, false );
    
    // Ready the expected results.
    String expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( oahuGridLabel );
    expected = expected.concat( blackGraphLine );
    expected = expected.concat( startLabels );
    expected = expected.concat( "0:|11-1|11-8|11-15|11-22|11-29|" );
    expected = expected.concat( "1:|0|50017218|100034435|150051653|200068870" );
    expected = expected.concat( "&amp;chds=0,200068870" );
    expected = expected.concat( "&amp;chd=t:189699346,184514706,200068870,184932310,184410244" );
    
    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking carbon week-based chart creation.", 
        results.compareTo(expected), 0 );
    
    // Get the results from the class.
    results = test.getChart( energy, "2009-11-01", 
        "2009-12-01", oahuGrid, "week", false, false );
    
    // Ready the expected results.
    expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( oahuGridLabel );
    expected = expected.concat( blackGraphLine );
    expected = expected.concat( startLabels );
    expected = expected.concat( "0:|11-1|11-8|11-15|11-22|11-29|" );
    expected = expected.concat( "1:|0|25500|50999|76499|101998" );
    expected = expected.concat( "&amp;chds=0,101998" );
    expected = expected.concat( "&amp;chd=t:101998,101998,101998,101998,101998" );
    
    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking energy week-based chart creation.", 
        results.compareTo(expected), 0 );
  }
  
  /**
   * Tests normal operation of the transitive option.
   */
  @Test
  public void testTransitiveOption() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-11-15", 
        "2009-11-16", "SIM_AES", hour, true, false );
    
    String expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( "&amp;chdl=SIM_AES" );
    expected = expected.concat( blackGraphLine );
    expected = expected.concat( startLabels );
    expected = expected.concat( hoursOfTheDay );
    expected = expected.concat( "1:|0|95400|190800|286200|381600" );
    expected = expected.concat( "&amp;chds=0,381600" );
    expected = expected.concat( "&amp;chd=t:381600,381600,381600,381600,381600,381600,381600," );
    expected = expected.concat( "381600,381600,381600,381600,381600,381600,381600," );
    expected = expected.concat( "381600,381600,381600,381600,381600,381600,381600," );
    expected = expected.concat( "381600,381600,381600" );
    
    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking transitive chart creation when given something with no sub-sources.", 
        results.compareTo(expected), 0 );
    
    results = test.getChart( carbon, "2009-11-15", 
        "2009-11-16", "SIM_IPP", hour, true, false );
    
    expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( "&amp;chdl=SIM_AES|SIM_HPOWER|SIM_KALAELOA|SIM_IPP" );
    expected = expected.concat( "&amp;chco=000000,990033,FF9900,FFCC00" );
    expected = expected.concat( startLabels );
    expected = expected.concat( hoursOfTheDay );
    expected = expected.concat( "1:|0|197900|395800|593700|791600" );
    expected = expected.concat( "&amp;chds=0,791600,0,791600,0,791600,0,791600" );
    expected = expected.concat( "&amp;chd=t:381600,381600,381600,381600,381600,381600,381600," );
    expected = expected.concat( "381600,381600,381600,381600,381600,381600," );
    expected = expected.concat( "381600,381600,381600,381600,381600,381600," );
    expected = expected.concat( "381600,381600,381600,381600,381600|" );
    expected = expected.concat( "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0|" );
    expected = expected.concat( "410000,410000,410000,410000,410000,410000,410000,410000," );
    expected = expected.concat( "410000,410000,410000,410000,410000,410000,410000,410000," );
    expected = expected.concat( "410000,410000,410000,410000,410000,410000,410000,410000|" );
    expected = expected.concat( "791600,791600,791600,791600,791600,791600,791600,791600," );
    expected = expected.concat( "791600,791600,791600,791600,791600,791600,791600,791600," );
    expected = expected.concat( "791600,791600,791600,791600,791600,791600,791600,791600" );
    
    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking normal transitive chart creation.", 
        results.compareTo(expected), 0 );
  }
  
  /**
   * Tests normal operation of the cumulative option.
   */
  @Test
  public void testCumulativeOption() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-11-17", 
        "2009-11-18", oahuGrid, hour, false, true );
    
    String expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( "&amp;chdl=SIM_OAHU_GRID|Cumulative" );
    expected = expected.concat( "&amp;chco=000000,990033" );
    expected = expected.concat( startLabels );
    expected = expected.concat( hoursOfTheDay );
    expected = expected.concat( "1:|0|7489868|14979736|22469604|29959472" );
    expected = expected.concat( "&amp;chds=0,29959472" );
    expected = expected.concat( "&amp;chd=t:1015700,1012100,1005800,1003100,1005800,1035880," );
    expected = expected.concat( "1088760,1126840,1170520,1232120,1265980,1302640,1341340," );
    expected = expected.concat( "1355740,1348540,1339540,1344940,1395940,1643360,1922816," );
    expected = expected.concat( "1729696,1241820,1019300,1011200|" );
    expected = expected.concat( "1015700,2027800,3033600,4036700,5042500,6078380,7167140," );
    expected = expected.concat( "8293980,9464500,10696620,11962600,13265240,14606580,15962320," );
    expected = expected.concat( "17310860,18650400,19995340,21391280,23034640,24957456," );
    expected = expected.concat( "26687152,27928972,28948272,29959472" );
    
    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking cumulative chart creation.", 
        results.compareTo(expected), 0 );
  }

  /**
   * Tests for proper response when given a bad source when using the transitive option.
   */
  @Test
  public void testBadSourceForTransitiveOption() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-11-19", 
        "2009-11-20", "SUPERMAN", hour, true, false );
    
    assertEquals( "Checking for correct response to a bad source for the transitive option.",
        results.compareTo( GoogleChartMaker.errorImage ), 0 );
  }
  
  /**
   * Tests for proper energy charting with the cumulative option.
   */
  @Test
  public void testEnergyChartingWithCumulativeOption() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( energy, "2009-11-21", "2009-11-22",
        oahuGrid, hour, false, true );
    
    // Ready the expected results.
    String expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( "&amp;chdl=SIM_OAHU_GRID|Cumulative" );
    expected = expected.concat( "&amp;chco=000000,990033" );
    expected = expected.concat( startLabels );
    expected = expected.concat( hoursOfTheDay );
    expected = expected.concat( "1:|0|3522|7045|10567|14089" );
    expected = expected.concat( "&amp;chds=0,14089" );
    expected = expected.concat( "&amp;chd=t:502,500,499,500,502,514,536,555,575,597,616,636," );
    expected = expected.concat( "655,663,661,659,659,659,658,654,631,593,554,517|" );
    expected = expected.concat( "502,1002,1501,2001,2503,3017,3552,4107,4682,5279,5894," );
    expected = expected.concat( "6530,7184,7847,8508,9166,9825,10484,11142,11795,12426," );
    expected = expected.concat( "13018,13572,14089" );
    
    // Compare the expected result and the actual result to see if they are the same.
    assertEquals( "Checking proper function of cumulative option with energy charting.", 
        results.compareTo(expected), 0 );
  }
  
  /**
   * Test the GoogleChartMaker for correct error handling when it would
   * normally create a URL size greater than is currently supported.
   */
  @Test
  public void testTooBigUrlHandling() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( null );
    // Get the results from the class.
    String results = test.getChart( carbon, "2009-11-19", 
        "2009-11-20", oahuGrid, hour, true, true );
    
    assertEquals( "Checking for correct error response to creating too big of a URL to use.",
        results.compareTo( GoogleChartMaker.overflowImage ), 0 );
  }

  /**
   * Tests for proper function of the server switching function.
   */
  @Test
  public void testServerSwitch() {
    // Create a new GoogleChartMaker for testing.
    GoogleChartMaker test = new GoogleChartMaker( "http://www.google.com/" );
    String results = test.getChart( carbon, "2009-11-21", 
        "2009-11-22", oahuGrid, hour, false, false );
    
    assertEquals( "Checking for correct response when directed to a non-WattDepot server.",
        results.compareTo( GoogleChartMaker.timeImage ), 0 );
    
    // Use a proper WattDepot server.
    test = new GoogleChartMaker( "http://server.wattdepot.org:8182/wattdepot/" );
    results = test.getChart( carbon, "2009-11-21", "2009-11-22",
        oahuGrid, hour, false, false );
    
    // Ready the expected results.
    String expected = "";
    expected = expected.concat( chartStart );
    expected = expected.concat( oahuGridLabel );
    expected = expected.concat( blackGraphLine );
    expected = expected.concat( startLabels );
    expected = expected.concat( hoursOfTheDay );
    expected = expected.concat( "1:|0|324092|648184|972276|1296368" );
    expected = expected.concat( "&amp;chds=0,1296368" );
    expected = expected.concat( "&amp;chd=t:1011200,1007600,1005800,1007600,1010300,1032800," );
    expected = expected.concat( "1071500,1106432,1140664,1179904,1212168,1247048,1281176," );
    expected = expected.concat( "1296368,1293008,1287408,1288528,1288528,1285416,1278440," );
    expected = expected.concat( "1238328,1172056,1104296,933738" );
    
    // Checking for good results.
    assertEquals( "Checking for normal operation from a good WattDepot server.", 
        results.compareTo(expected), 0 );
  }
}
